<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<section class="content-header" style="">
    <h1>
        文件上传页面
        <small>文件上传管理页面</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
        <li><a href="#">文件上传管理</a></li>
        <li class="active">文件上传管理</li>
    </ol>
</section>

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <!-- /.box-header -->
                <div class="box-body">
                    <div id="toolbar">
                        <div class="btn-group btn-group-sm">
                            <!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
                            <button  type="button" id="addBtn" class="btn btn-default"
                                    data-toggle="modal"
                                    data-target="#addModal"><i class="glyphicon glyphicon-plus"></i> 新增
                            </button>
                            <!-- Indicates caution should be taken with this action -->
                            <button  type="button" id="editBtn" class="btn btn-default"
                                    data-toggle="modal"
                                    data-target="#editModal"
                                    data-action="{type:'editable',form:'#editForm',table:'#table',after:'editAfter'}"
                                    disabled><i class="glyphicon glyphicon-edit"></i> 修改
                            </button>
                            <!-- Indicates a dangerous or potentially negative action -->
                            <button type="button" id="deleteBtn" class="btn btn-default" data-toggle="modal"
                                    data-target="#deleteModal"
                                    data-action="{type:'delete',form:'#deleteForm',idField:'id',table:'#table'}"
                                    disabled><i class="glyphicon glyphicon-remove"></i> 删除
                            </button>
                        </div>
                    </div>
                    <table id="table"></table>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->

        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->
</section>
<!-- /.content -->

<!-- add Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="addModalLabel">上传页面</h4>
            </div>
            <div class="modal-body">
                <form id="addForm" method="POST" enctype="multipart/form-data">
                    <input type="hidden" id="uploadjson" name="uploadjson">
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="uploadfile"><span
                                class="asterisk">*</span>上传文件:</label>
                        <div class="col-sm-10 controls">
                            <input type="file" multiple="multiple" id="uploadfile" name="uploadfile" data-show-caption="true" />
                            <div class="help-block with-errors"></div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="submit" form="addForm" class="btn btn-primary"
                        data-action="{type:'submit',form:'#addForm',url:'@{/fileserver/create}',after:'$.myAction.refreshTable'}">
                    确定
                </button>
            </div>
        </div>
    </div>
</div>

 <div id="actionButtons" class="admin-hide">
        <a class="like" href="javascript:void(0)" data-toggle="tooltip" title="Like"><i class="glyphicon glyphicon-heart"></i></a>　
        <a  class="edit ml10" href="javascript:void(0)" data-toggle="tooltip" title="下载"><i class="glyphicon glyphicon-edit"></i></a>　
    </div>
    </div>
</div>

<script th:inline="javascript">
    var $table = $('#table');

    $(function () {
        // bootstrap table初始化
        // http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/
        $table.bootstrapTable({
            url: CONTEXT_PATH + '/fileserver/list',
            idField: 'id',
            columns: [
                {field: 'state', checkbox: true},
                {field: 'id', title: '编号', sortable: true, halign: 'left'},
                {field: 'username', title: '用户', sortable: true, halign: 'left'},
                {field: 'ip', title: '上传地址', sortable: true, halign: 'left'},
                {field: 'sourceName', title: '源文件', sortable: true, halign: 'left'},
                {field: 'fileType', title: '文件类型', sortable: true, halign: 'left'},
                {field: 'fileSize', title: '文件大小', sortable: true, halign: 'left'},
                {field: 'tagPathFile', title: '存储路径', sortable: true, halign: 'left'},
                {field: 'tagUrlPath', title: 'url路径', sortable: true, halign: 'left'},
                {
                    field: 'action',
                    title: '操作',
                    halign: 'center',
                    align: 'center',
                    formatter: 'actionFormatter',
                    events: 'actionEvents',
                    clickToSelect: false
                }
            ]
        });
     <!--上传组件初始化-->
        initFileInput("uploadfile");
    });

    // 数据表格展开内容
    function detailFormatter(index, row) {
        var html = [];
        $.each(row, function (key, value) {
            html.push('<p><b>' + key + ':</b> ' + value + '</p>');
        });
        return html.join('');
    }

    function editAfter(obj, row) {
        <!--一个用户有几个角色，几个组-->
        console.log("点击update按钮后")
    }

    function actionFormatter(value, row, index) {
        return $('#actionButtons').html();
    }

    window.actionEvents = {
        'click .like': function (e, value, row, index) {
            alert('You click like icon, row: ' + JSON.stringify(row));
            console.log(value, row, index);
        },
        'click .edit': function (e, value, row, index) {
            $('#editModal').modal('show')
            $('#editForm').fillForm(row);
            editAfter('', row);
            console.log(value, row, index);
        },
        'click .remove': function (e, value, row, index) {
            $('#deleteModal').modal('show');
            $('.records').html('1');
            var html = '';
            for (var key in row) {
                html += '<input type="hidden" name="' + key + '" value="' + row[key] + '">';
            }
            $('#deleteForm').html(html);
        }
    };

    function initFileInput(ctrlName) {
        var uploadfilemap = {};
        var control = $('#' + ctrlName);
        control.fileinput({
            language: 'zh', //设置语言
            uploadUrl: CONTEXT_PATH + '/fileserver/upload', //上传的地址
            allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
            uploadAsync: true, //默认异步上传
            showUpload: true, //是否显示上传按钮
            showRemove: true, //显示移除按钮
            showPreview: true, //是否显示预览
            showCaption: false,//是否显示标题
            browseClass: "btn btn-primary", //按钮样式
            maxFileCount: 4, //允许同时上传的最大文件个数
            enctype: 'multipart/form-data',
            validateInitialCount: true,
            msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
            uploadExtraData: function (previewId, index) {
                //传参
                var data = {
                    "previewId": previewId,      //此处自定义传参
                    "inputId": ctrlName,//上传input的id
                    "destDir": '/user/excel'
                };
                return data;
            }

        }).on('filepreupload', function (event, data, previewId, index) {     //上传中
            console.log('文件正在上传:');
        }).on("fileuploaded", function (event, data, previewId, index) {    //一个文件上传成功
            var form = data.form, files = data.files, extra = data.extra,
                response = data.response, reader = data.reader;
            //将上传文件的返回数据存放在map中
            //uploadfilemap[previewId] = response.data;
            uploadfilemap[response.data.previewId] = response.data;
            console.log("上传图片后json=：" + JSON.stringify(uploadfilemap));
            $("#uploadjson").val(JSON.stringify(uploadfilemap));
            // console.log("上传图片id："+index+","+"previewId="+previewId+","+response);//打印出返回的json
            //console.log(response.code);//返回状态
        }).on('fileerror', function (event, data, msg) {  //一个文件上传失败
            console.log('文件上传失败！' + data.status);
        }).on('filesuccessremove', function (event, data, previewId, index) {
            $.each(uploadfilemap, function (_key) {
                var key = _key;
                var value = uploadfilemap[_key];
                if (_key == data) {  //删除
                    delete uploadfilemap[_key];
                }
            });
            console.log("删除图片后的json：" + JSON.stringify(uploadfilemap));
            //console.log('Uploaded thumbnail successfully removed,id='+index+"data="+data);
            $("#uploadjson").val(JSON.stringify(uploadfilemap));
        });

    }
</script>
</html>
